//
//  JYBaseStatics.m
//  kunlun
//
//  Created by zhusanbao on 2020/9/25.
//  Copyright © 2020 zhusanbao. All rights reserved.
//

#import "JYBaseStatics.h"
#import "UIColor+YYAdd.h"
#import "NSTimer+YYAdd.h"
#import "NSBundle+YYAdd.h"

#define kPingFangRegular    @"PingFangSC-Regular"
#define kPingFangMedium     @"PingFangSC-Medium"
#define kPingFangThin       @"PingFangSC-Thin"
#define kPingFangSemibold   @"PingFangSC-Semibold"
#define kPingFangLight      @"PingFangSC-Light"
#define kPingFangUltralight @"PingFangSC-Ultralight"

#define kArialBold          @"Arial-BoldMT"
#define kAlternateBold      @"DINAlternate-Bold"

@implementation JYBaseStatics

#pragma --mark UIFont 定义
static inline UIFont *_PingFangFont(NSString *fontName, CGFloat fontSize){
    if (fontName.length == 0) return nil;
    return [UIFont fontWithName:fontName size:fontSize];
}

+ (UIFont *)kl_systemFontOfSize:(CGFloat)fontSize {
    return _PingFangFont(kPingFangRegular, fontSize) ?: [UIFont systemFontOfSize:fontSize];
}
+ (UIFont *)kl_boldSystemFontOfSize:(CGFloat)fontSize {
    return _PingFangFont(kPingFangMedium, fontSize) ?: [UIFont boldSystemFontOfSize:fontSize];
}

UIFont *U_font(CGFloat fontSize){
    return [JYBaseStatics kl_systemFontOfSize:fontSize];
}
//UIFont *kl_mfont(CGFloat fontSize){
//    return [JYBaseStatics kl_boldSystemFontOfSize:fontSize];
//}
UIFont *U_bfont(CGFloat fontSize){
    return [UIFont systemFontOfSize:fontSize weight:UIFontWeightBold];
}

UIFont *U_sfont(CGFloat fontSize){
    return [UIFont systemFontOfSize:fontSize weight:UIFontWeightSemibold];
}

UIFont *jy_atbfont(CGFloat fontSize){
    return _PingFangFont(kAlternateBold, fontSize) ?: [UIFont boldSystemFontOfSize:fontSize];;
}
UIColor *jy_HexColor(NSString *cl){
    if (cl.length == 0) return UIColor.clearColor;
#if DEBUG
    UIColor *color = [UIColor colorWithHexString:cl];
    NSCAssert(color != nil, @"获取Color为空 : %@",cl);
    return color ?: UIColor.clearColor;
#else
    return [UIColor colorWithHexString:cl] ?: UIColor.clearColor;
#endif
}
UIColor *jyRandomColor(void){
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
UIImage *U_imageNamed(NSString *im){
    return im ? [UIImage imageNamed:im] : nil;
}
UIImage *jy_imageRenderNamed(NSString *im,UIImageRenderingMode mode){
    return im ? [[UIImage imageNamed:im] imageWithRenderingMode:mode] : nil;
}
UIImage *jy_imageContentFile(NSString *fp){
    return fp ? [UIImage imageWithContentsOfFile:fp] : nil;
}
//从Images.bundle加载图片
UIImage * _Nullable jy_imageFromImagesBundle(NSString * _Nonnull imageName, NSString *_Nullable type){
    if (imageName) {
        NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"Images" ofType:@"bundle"]];
        return [UIImage imageWithContentsOfFile:[bundle pathForScaledResource:(imageName) ofType:(type ?: @"png")]];
    }
    return nil;
}
UIImage *jy_imageRenderContentFile(NSString *fp, UIImageRenderingMode mode){
    return fp ? [[UIImage imageWithContentsOfFile:fp] imageWithRenderingMode:mode] : nil;
}
UIImage * _Nullable jy_imageFromBundleForType(NSString *bundleName, NSString * _Nonnull imageName, NSString *_Nullable type){
    if (imageName) {
        NSBundle *bundle = nil;
        if (bundleName) {
            bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:bundleName ofType:@"bundle"]];
        }else{
            bundle = NSBundle.mainBundle;
        }
        return [UIImage imageWithContentsOfFile:[bundle pathForScaledResource:(imageName) ofType:(type ?: @"png")]];
    }
    return nil;
}
UIImage * _Nullable jy_imageFromBundle(NSString *bundleName, NSString * _Nonnull imageName){
    return jy_imageFromBundleForType(bundleName, imageName, @"png");
}
NSTimer *dispatch_count_down(dispatch_time_t dur,id host, void (^block)(NSTimer * _Nonnull tm, id host, NSTimeInterval now)){
    if (block) {
        __block NSTimeInterval cidx = dur;
        __weak typeof(host)wkHost = host;
        NSTimer *t = [NSTimer scheduledTimerWithTimeInterval:1 block:^(NSTimer * _Nonnull timer) {
            if (block) {
                block(timer, wkHost, --cidx);
                if (!wkHost || cidx == 0) [timer invalidate];
            }else{
                [timer invalidate];
            }
        } repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:t forMode:NSRunLoopCommonModes];
        return t;
    }
    return nil;
}

NSDictionary *jy_app_Info(void){
    static NSDictionary *info = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        info = [[NSBundle mainBundle] infoDictionary];
    });
    return info;
}

NSString *jy_formatTimeFromDuration(long long duration){
    long long hour = 0,min = 0,second = 0;
    if (duration>=60*60) {
        hour = duration/3600;
        min = (duration-hour*3600)/60;
        second = duration-hour*3600-min*60;
        return [NSString stringWithFormat:@"%.2lld:%.2lld:%.2lld",hour,min,second];
    }else if(duration>60&&duration<3600){
        min = duration/60;
        second = duration-min*60;
    }else{
        second = duration;
    }
    return [NSString stringWithFormat:@"%.2lld:%.2lld",min,second];
}
NSString *jy_formatDigital(NSInteger d){
    if (d < 1000) {
        return [NSString stringWithFormat:@"%ld",d];
    }else if (d < 10000){
        return [NSString stringWithFormat:@"%.1fk",d/1000.0];
    }else{
        return [NSString stringWithFormat:@"%.1fw",d/10000.0];
    }
}

@end
